package com.mlong.admin.modular.unicomjs.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kordar.admin.enums.CommonActive;
import com.kordar.admin.enums.EnumUtils;
import com.kordar.admin.kernel.wrapper.BatchDeleteWrapperEntity;
import com.kordar.admin.kernel.wrapper.UpdateWrapperEntity;
import com.kordar.datalist.DatalistService;
import com.kordar.datalist.ResultEntity;
import com.mlong.admin.modular.unicomjs.entity.Goods;
import com.mlong.admin.modular.unicomjs.entity.GoodsType;
import com.mlong.admin.modular.unicomjs.service.GoodsService;
import com.mlong.admin.modular.unicomjs.service.GoodsTypeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@AllArgsConstructor
@Controller
@RequestMapping("unicomjs")
public class GoodsController {

    private final GoodsService goodsService;
    private final GoodsTypeService goodsTypeService;
    private final DatalistService datalistService;

    @ModelAttribute("activeLabels")
    public Map<Integer, String> active() {
        return EnumUtils.EnumToMapInt(CommonActive.class);
    }

    @ModelAttribute("pidLabels")
    public Map<Long, String> types() {
        return goodsTypeService.list().stream().collect(Collectors.toMap(GoodsType::getId, GoodsType::getName));
    }

    @GetMapping("goods/index")
    public String index(Model model) {
        model.addAttribute("data", datalistService.render(goodsService.list(), Goods.class));
        return "pages/modular/unicomjs/goods/index";
    }

    @GetMapping("goods/form")
    public String form(Model model, @RequestParam(required = false, defaultValue = "") String id) {
        if ("".equals(id)) {
            model.addAttribute("header", "新建产品");
            model.addAttribute("model", new Goods());
        } else {
            model.addAttribute("header", "修改产品");
            model.addAttribute("isUpdate", true);
            model.addAttribute("model", goodsService.getById(id));
        }

        return "pages/modular/unicomjs/goods/form";
    }

    @PostMapping("goods/save")
    @ResponseBody
    public ResultEntity<Object> save(@Validated Goods goods) {
        goodsService.saveOrUpdate(goods);
        return ResultEntity.success();
    }

    @RequestMapping("goods/edit")
    @ResponseBody
    public ResultEntity<Object> edit(@RequestBody UpdateWrapperEntity<Goods> entity) {
        UpdateWrapper<Goods> query = entity.where(Goods.class).data().getQuery();
        goodsService.update(query);
        return ResultEntity.success();
    }

    @PostMapping("goods/delete")
    @ResponseBody
    public ResultEntity<Object> del(String id) {
        goodsService.removeById(id);
        return ResultEntity.success();
    }

    @RequestMapping("goods/batch-delete")
    @ResponseBody
    public ResultEntity<Object> del(@RequestBody BatchDeleteWrapperEntity<String> entity) {
        Set<String> ids = entity.getIds();
        goodsService.removeByIds(ids);
        return ResultEntity.success();
    }

}
